package com.dgut.finaldemo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.finaldemo.entity.Teacher;
import com.dgut.finaldemo.mapper.TeacherMapper;
import com.dgut.finaldemo.vo.DataVO;
import com.dgut.finaldemo.vo.TeacherVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TeacherServiceImpl implements TeacherService{
    @Autowired
    private TeacherMapper teacherMapper;

    @Override
    public DataVO<Teacher> findData(Integer page, Integer limit) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        IPage<Teacher> teacherIPage = new Page<>(page,limit);
        IPage<Teacher> result = teacherMapper.selectPage(teacherIPage,null);

        dataVO.setCount(result.getTotal());

        List<Teacher> teacherList = result.getRecords();
        List<TeacherVO> teacherVOList = new ArrayList<>();
        for (Teacher teacher:teacherList) {
            TeacherVO teacherVO = new TeacherVO();
            BeanUtils.copyProperties(teacher,teacherVO);
            if(teacher.getStatus()==1){
                teacherVO.setStatus("正常");
            }else{
                teacherVO.setStatus("停用");
            }
            teacherVOList.add(teacherVO);
        }
        dataVO.setData(teacherVOList);
        return dataVO;

    }

    @Override
    public void addTeacher(Teacher teacher) {
        teacherMapper.insert(teacher);
    }

    @Override
    public Teacher queryTeacherById(Integer tid) {
        Teacher teacher = teacherMapper.selectById(tid);
        return teacher;
    }

    @Override
    public void updateTeacher(Teacher teacher) {
        teacherMapper.updateById(teacher);
    }

    @Override
    public void deleteTeacherById(Integer tid) {
        teacherMapper.deleteById(tid);
    }

    @Override
    public DataVO<Teacher> searchTeacher(Integer page, Integer limit, Teacher teacher) {
        DataVO dataVO = new DataVO();
        dataVO.setCode(0);
        dataVO.setMsg("");

        if(teacher.getUsername()==null)
            teacher.setUsername("");
        if(teacher.getName()==null)
            teacher.setName("");
        if(teacher.getCareer()==null)
            teacher.setCareer("");
        if(teacher.getPhone()==null)
            teacher.setPhone("");
        if(teacher.getAddress()==null)
            teacher.setAddress("");
        if(teacher.getClock()==null)
            teacher.setClock("");

        IPage<Teacher> teacherIPage = new Page<>(page,limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        if(teacher.getTid()==null){
            queryWrapper
                    .like("username",teacher.getUsername())
                    .like("name",teacher.getName())
                    .like("career",teacher.getCareer())
                    .like("phone",teacher.getPhone())
                    .like("address",teacher.getAddress())
                    .like("clock",teacher.getClock());
        }else{
            queryWrapper
                    .eq("tid",teacher.getTid())
                    .like("username",teacher.getUsername())
                    .like("name",teacher.getName())
                    .like("career",teacher.getCareer())
                    .like("phone",teacher.getPhone())
                    .like("address",teacher.getAddress())
                    .like("clock",teacher.getClock());
        }

        IPage<Teacher> result = teacherMapper.selectPage(teacherIPage,queryWrapper);

        dataVO.setCount(result.getTotal());

        List<Teacher> teacherList = result.getRecords();
        List<TeacherVO> teacherVOList = new ArrayList<>();
        for (Teacher teacher1:teacherList) {
            TeacherVO teacherVO = new TeacherVO();
            BeanUtils.copyProperties(teacher1,teacherVO);
            if(teacher1.getStatus()==1){
                teacherVO.setStatus("正常");
            }else{
                teacherVO.setStatus("停用");
            }
            teacherVOList.add(teacherVO);
        }
        dataVO.setData(teacherVOList);
        return dataVO;
    }

    @Override
    public Teacher queryTeacherByUsername(String username) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        return teacher;
    }
}
